package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.widget.GestureImageView;

public class CompanyForumPictureFragment extends BaseFragment {

  public static final String EXTRA_PICTUREA = "com.isunland.managesystem.ui.EXTRA_PICTUREA";

  public static final String EXTRA_PICTUREB = "com.isunland.managesystem.ui.EXTRA_PICTUREB";

  public static final String EXTRA_PICTUREC = "com.isunland.managesystem.ui.EXTRA_PICTUREC";

  public static final String EXTRA_PICTURED = "com.isunland.managesystem.ui.EXTRA_PICTURED";

  public static final String TYPE = "com.isunland.managesystem.ui.TYPE";

  public static final String TYPEA = "com.isunland.managesystem.ui.TYPEA";

  public static final String TYPEB = "com.isunland.managesystem.ui.TYPEB";
  public static final String TYPEC = "com.isunland.managesystem.ui.TYPEC";
  public static final String TYPED = "com.isunland.managesystem.ui.TYPED";

  private String mPictureA;
  private String mPictureB;
  private String mPictureC;
  private String mPictureD;
  private String mType;
  private ImageLoader mImageLoader;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.pictureDetail);
    mImageLoader = RequestManager.getImageLoader();
    mPictureA = getArguments().getString(EXTRA_PICTUREA);
    mPictureB = getArguments().getString(EXTRA_PICTUREB);
    mPictureC = getArguments().getString(EXTRA_PICTUREC);
    mPictureD = getArguments().getString(EXTRA_PICTURED);
    LogUtil.i("mPictureA\\\\" + mPictureA);
    LogUtil.i("mPictureB\\\\" + mPictureB);
    LogUtil.i("mPictureC\\\\" + mPictureC);
    LogUtil.i("mPictureD\\\\" + mPictureD);
    mType = getArguments().getString(TYPE);
    if (mActivity.getSupportActionBar() != null) {
      mActivity.getSupportActionBar().hide();
    }
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_forum_picture, container, false);
    GestureImageView mIvForumPicture = (GestureImageView) view.findViewById(R.id.iv_forum_picture);
    //点击事件与子view冲突.
    //View llHolder = view.findViewById(R.id.ll_imageHolder);
    //llHolder.setOnClickListener(new View.OnClickListener() {
    //    @Override
    //    public void onClick(View v) {
    //        getActivity().finish();
    //    }
    //});
    //mIvForumPicture.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    if (TYPEA.equals(mType)) {
      if (!TextUtils.isEmpty(mPictureA)) {
        String urlImage = ApiConst.getDownLoadUrlByGet(mPictureA);
        ImageListener listener = ImageLoader.getImageListener(mIvForumPicture, // 控件名
            0, 0); // 默认图片
        mImageLoader.get(urlImage, listener);
      }
    } else if (TYPEB.equals(mType)) {
      if (!TextUtils.isEmpty(mPictureB)) {
        String urlImage = ApiConst.getDownLoadUrlByGet(mPictureB);
        ImageListener listener = ImageLoader.getImageListener(mIvForumPicture, // 控件名
            0, 0); // 默认图片
        mImageLoader.get(urlImage, listener);
      }
    } else if (TYPEC.equals(mType)) {
      if (!TextUtils.isEmpty(mPictureC)) {
        String urlImage = ApiConst.getDownLoadUrlByGet(mPictureC);
        ImageListener listener = ImageLoader.getImageListener(mIvForumPicture, // 控件名
            0, 0); // 默认图片
        mImageLoader.get(urlImage, listener);
      }
    } else if (TYPED.equals(mType)) {
      if (!TextUtils.isEmpty(mPictureD)) {
        String urlImage = ApiConst.getDownLoadUrlByGet(mPictureD);
        ImageListener listener = ImageLoader.getImageListener(mIvForumPicture, // 控件名
            0, 0); // 默认图片
        mImageLoader.get(urlImage, listener);
      }
    }

    return view;
  }

  public static Fragment newInstance(String pictureA, String pictureB, String pictureC,
      String pictureD, String type) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_PICTUREA, pictureA);
    bundle.putSerializable(EXTRA_PICTUREB, pictureB);
    bundle.putSerializable(EXTRA_PICTUREC, pictureC);
    bundle.putSerializable(EXTRA_PICTURED, pictureD);
    bundle.putSerializable(TYPE, type);
    CompanyForumPictureFragment fragment = new CompanyForumPictureFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  /**
   * 响应ActionBar中的按钮
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮

      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
